package Vue;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Control.Args;
import Control.MonThread;
import Control.MonteCarlo;
import Control.Fichier;

public class MonteCarloFrame extends JFrame{
	public MonteCarloFrame(){
		super();
		build();
	}
	
	public void build(){
		setTitle("MonteCarlo");
		setSize(800,300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(buildContentPane());
	}
	
	private JComboBox values;
	private JTextField result;
	private JTextField result1;
	private String lastrecup = null;
	private List<Args> tabargs;
	private double valfin = 0;
	private double valthread = 0;
	
	private JPanel buildContentPane(){
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		
		tabargs = new ArrayList<Args>();
		Fichier f = new Fichier();
		try {
			tabargs = f.loadfichier("test.txt");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		List<String> test = new ArrayList<String>();
		test.add("");
		for(int i=0; i<tabargs.size(); i++){
			test.add(tabargs.get(i).toString());
		}
		
		final MonteCarlo mc = new MonteCarlo(0, 0, 0, 0, 0, 0, 0, 0);
		
		values = new JComboBox(test.toArray());
		result = new JTextField();
		result1 = new JTextField();
				
		panel.add(values);
				
		values.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				// TODO Auto-generated method stub
				String recup = (String)values.getSelectedItem();
				if(recup != lastrecup){
					lastrecup=recup;
					System.out.println(recup);
					for(int i=0; i<tabargs.size(); i++){
						if(tabargs.get(i).toString().compareTo(recup)==0){
							valfin = mc.MonteCarlo(tabargs.get(i).getCallPutFlag(), tabargs.get(i).getS(), tabargs.get(i).getX(), tabargs.get(i).getT(), tabargs.get(i).getR(), tabargs.get(i).getB(), tabargs.get(i).getV(), tabargs.get(i).getnSteps(), tabargs.get(i).getnSimulations());
							MonThread mt = new MonThread(tabargs.get(i).getCallPutFlag(), tabargs.get(i).getS(), tabargs.get(i).getX(), tabargs.get(i).getT(), tabargs.get(i).getR(), tabargs.get(i).getB(), tabargs.get(i).getV(), tabargs.get(i).getnSteps(), tabargs.get(i).getnSimulations());
								try {
									valthread = mt.compute();
								} catch (InterruptedException
										| ExecutionException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							result.setText(Double.toString(valfin));
							result1.setText(Double.toString(valthread));
							System.out.println("multi thread: "+valthread);
							break;
						}
					}
					
				}
			}
		});
		
		panel.add(result);
		panel.add(result1);
		
		return panel;
	}	
		
}
